<?php
class Workout extends AppModel {
	var $name = 'Workout';
	var $validate = array(
		'person_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'name' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'duration' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'calories_burned' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);
	//The Associations below have been created with all possible keys, those that are not needed can be removed

	var $belongsTo = array(
		'Person' => array(
			'className' => 'Person',
			'foreignKey' => 'person_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);

	var $hasMany = array(
		'Point' => array(
			'className' => 'Point',
			'foreignKey' => 'workout_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);
	
	// no longer used
	public function addWorkoutWithPoints($wData) {
	
		/*
		[Workout] => Array
        (
            [person_id] => 14
            [name] => test
            [date] => Array
                (
                    [month] => 01
                    [day] => 25
                    [year] => 2013
                )

            [duration] => 30
            [calories_burned] => 30
        )
		*/
		
		// Initialize our settings values as psudo constants
		// $WORKOUT_MODIFIER_DURATION_MIN
		foreach(ClassRegistry::init('Setting')->find('all') as $s)
			extract(array($s['Setting']['name'] => $s['Setting']['value']));
		
		// use extract on the data arrays to make calc readability easier
		// $person_id, $name, $date['month'], $date['day'], $date['year'], $duration
		extract($wData['Workout']);
		
		// start the db transaction (enables rollback if save fails)
		$ds = $this->getDataSource();
		$ds->begin($this);
		
		$wData['Workout']['calculated'] = 1;
		
		if($this->save($wData)) {		
			// base points = input calories
			$points = $calories_burned; 				
			// if duration flag used, give 20% bonus
			$points *= ($duration >= $WORKOUT_MODIFIER_DURATION_MIN) ? 1.2 : 1 ; 	
			
			// build data array for saving the points for this meal
			$data['Point'] = array(
				'person_id' => $person_id,
				'workout_id' => $this->id,
				'points' => $points
			);
			
			$Point = ClassRegistry::init('Point');
			if($Point->save($data)) {
				//success
				return $ds->commit($this) !== false; // register the db transaction, or return false if error here
			}
		}
		// rollback
		$ds->rollback($this); // if we didn't strike the commit we rollback on the transaction
		return false;
	}	
}
?>